﻿using System;
using System.IO;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
 
namespace Test.Testitextsharp {
    public class TestItextsharp {
        
        #region 打印费用清单
        public void BtnPrint() {
            Document myDocument = new Document(PageSize.A4,50,50,50,50);
            try {
                string patientName = "张三";
                string patientNumber = "三号";
                string sections = "外科";
                string doctorName = "李四";
                string cashier = "王五";

                PdfWriter.GetInstance(myDocument,new FileStream("FeeList.pdf",FileMode.Create));
                myDocument.Open();
                string fontPath = @"C:\Windows\Fonts\simfang.ttf";//强制中文字体，否则无法显示中文
                BaseFont baseFont = BaseFont.CreateFont(fontPath,BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);

                //标题居中
                Paragraph paragraph = new Paragraph("检验结果",new Font(baseFont,16));
                paragraph.Alignment = Element.ALIGN_CENTER;
                myDocument.Add(paragraph);
                paragraph =
                    new Paragraph(
                        "\n姓名："+patientName+"     日期："+   "2017/5/3" +  "     门诊号：" +  patientNumber+   "     科室："+
                        sections +  "     医生：" +  doctorName +  "     收费员：" +  cashier +  "\n\n",new Font(baseFont,9));
                paragraph.Alignment = Element.ALIGN_LEFT;
                myDocument.Add(paragraph);

                //添加一个表格
                PdfPTable pdfPTable = new PdfPTable(3);
                pdfPTable.TotalWidth = myDocument.Right - myDocument.Left;
                float[] widths = { 30f,30f,50f};
                pdfPTable.SetWidths(widths);
                pdfPTable.LockedWidth = true;                //必须锁定宽度，否则修改无效

                PdfPCell pdfPCell;
                pdfPCell = new PdfPCell(new Phrase("样本编号",new Font(baseFont,9)));
                pdfPTable.AddCell(pdfPCell);
                pdfPCell = new PdfPCell(new Phrase("项目",new Font(baseFont,9)));
                pdfPTable.AddCell(pdfPCell);
                pdfPCell = new PdfPCell(new Phrase("测试结果",new Font(baseFont,9)));
                pdfPTable.AddCell(pdfPCell);
                myDocument.Add(pdfPTable);

                ////增加详细数据
                //for(int i = 0;i < 6 - 1;i++) {
                //    for(int j = 0;j < 6;j++) {
                //        pdfPCell = new PdfPCell(new Phrase("",new Font(baseFont,9)));
                //        pdfPTable.AddCell(pdfPCell);
                //    }
                //}
                
                //paragraph = new Paragraph("\n金额合计：" +  "0",new Font(baseFont,9));
                //paragraph.Alignment = Element.ALIGN_CENTER;
                //myDocument.Add(paragraph);
                
                myDocument.Close();

                MessageBox.Show(@"打印成功，点击确定打开文件",@"成功",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
                try {
                    System.Diagnostics.Process.Start("FeeList.pdf");
                } catch(Exception ex) {
                    MessageBox.Show(ex.Message,@"出错",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
                }
            } catch(Exception ex) {
                MessageBox.Show(ex.Message,@"出错",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
            }
        }
        #endregion
    }
}


